package com.hs.hlw.credit.system.web.controller.api;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.service.SubmissionBatchService;
import com.hs.hlw.credit.system.common.util.DateTimeUtils;
import com.hs.hlw.credit.system.data.DataUtils;
import com.hs.hlw.credit.system.data.mybatis.Pageable;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * SubmissionBatchController
 * @author zhb
 * @date 2018/11/26
 */
@RestController
@RequestMapping("/api/submission/batch")
public class SubmissionBatchController {
    private @Autowired SubmissionBatchService submissionBatchService;

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除批次")
    public ResponseDto delete(@RequestParam @ApiParam(value = "批次id", required = true) Integer id) {
        return submissionBatchService.delete(id);
    }

    @RequestMapping(value = "downloadBatchFile", method = RequestMethod.GET)
    @ApiOperation(value = "批次文件下载")
    public void downloadBatchFile(@RequestParam @ApiParam(value = "批次id", required = true) Integer id,
                                   HttpServletResponse response) {
        submissionBatchService.downloadBatchFile(id, response);
    }

    @GetMapping(path = "page")
    @ApiOperation(value = "分页查询报送批次", notes = "分页查询报送批次")
    public ResponseDto page(@RequestParam @ApiParam(value = "表单id", required = true) Integer formId,
                            @RequestParam(required = false) @ApiParam(value = "开始时间(时间格式为: yyyy-MM-dd)") String startTime,
                            @RequestParam(required = false) @ApiParam(value = "结束时间(时间格式为: yyyy-MM-dd)") String endTime,
                            @RequestParam(required = false) @ApiParam(value = "报送方式(1: 表单, 2:批量)") Short type,
                            @RequestParam(required = false) @ApiParam(value = "状态(0: 待处理, 1:处量中, 2: 处理异常, 3: 处理完成)") Short status,
                            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "查询页码", defaultValue = "1") Integer page,
                            @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页数据条数", defaultValue = "10") Integer size,
                            @RequestParam(required = false, defaultValue = "id") @ApiParam(value = "排序字段（多个用逗号隔开）", defaultValue = "id") String orderField,
                            @RequestParam(required = false, defaultValue = "desc") @ApiParam(value = "排序规则（asc:升序，desc:降序，多个用逗号隔开）", defaultValue = "desc") String direction) {
        Pageable pageable = DataUtils.pageable(page, size, orderField, direction);
        if (StringUtils.isNotBlank(startTime)) {
            pageable.addSomeParam("startTime", DateTimeUtils.getStartTimeOfDate(DateTimeUtils.stringToDate(startTime, DateTimeUtils.YYYY_MM_DD)));
        }
        if (StringUtils.isNotBlank(endTime)) {
            pageable.addSomeParam("endTime", DateTimeUtils.getEndTimeOfDate(DateTimeUtils.stringToDate(endTime, DateTimeUtils.YYYY_MM_DD)));
        }
        pageable.addParam("formId", formId);
        pageable.addParam("type", type);
        pageable.addParam("status", status);
        return submissionBatchService.page(pageable);
    }
}